'use strict';

angular.module('hanxinBroker').controller('LoginCtrl', function ($ionicHistory, Login, AuthedTransitionService, $state, $location) {
  var vm = this;
  vm.form = {};
  vm.loginFailed = undefined;

  vm.checkUsername = function checkUsername() {
    vm.loginFailed = undefined;
    if (!vm.form.username) {
      vm.form.usernameMessage = "此项为必填项";
    } else {
      vm.form.usernameMessage = undefined;
    }
  };

  vm.checkPassword = function checkPassword() {
    vm.loginFailed = undefined;
    if (!vm.form.password) {
      vm.form.passwordMessage = "此项为必填项";
    } else {
      vm.form.passwordMessage = undefined;
    }
  };

  vm.login = function login() {
    if (!vm.form.usernameMessage && !vm.form.passwordMessage) {
      Login.save(vm.form, function () {
        console.log(AuthedTransitionService.getState());
        $location.path(AuthedTransitionService.getState());
      }, function () {
        vm.loginFailed = "用户名或密码错误";
      })
    }
  };

});
